// Serializer.cs
// 
// Copyright (C) 2008 Jean-André Santoni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Astrognome
{
	public abstract class Serializer
	{		
		public static void SerializeConfig (Config config)
		{
			Stream stream = File.Open(".astrognome.config", FileMode.Create);
			BinaryFormatter bf = new BinaryFormatter();
			bf.Serialize(stream, config);
			stream.Close();
		}
		
		public static Config DeSerializeConfig()
		{
			Config config;
			Stream stream = File.Open(".astrognome.config", FileMode.Open);
			BinaryFormatter bf = new BinaryFormatter();
			config = (Config) bf.Deserialize(stream);
			stream.Close();
			
			return config;
		}
		
		public static void SerializeChart (string filename, Chart chart)
		{
			Stream stream = File.Open(filename, FileMode.Create);
			BinaryFormatter bf = new BinaryFormatter();
			bf.Serialize(stream, chart);
			stream.Close();
			
			AddToRecent(filename, chart);
		}

		public static Chart DeSerializeChart(string filename)
		{
			Chart chart;
			Stream stream = File.Open(filename, FileMode.Open);
			BinaryFormatter bf = new BinaryFormatter();
			chart = (Chart) bf.Deserialize(stream);
			stream.Close();
			
			AddToRecent(filename, chart);
			
			return chart;
		}
		
		private static void AddToRecent(string filename, Chart chart)
		{
			Gtk.RecentManager mgr = new Gtk.RecentManager();
			Gtk.RecentData data = new Gtk.RecentData();
			data.MimeType = "application/x-chart";
			data.AppName = "Astrognome";
			data.AppExec = "Astrognome";
			data.Description = chart.Name + "'s Natal Chart";
			mgr.AddFull("file://" + filename, data);
		}
	}
}
